<template>
    <a-spin :spinning="spinning">
        <table-search @search="search(1)" :isExtra="false" @reset="reset()">
            <template slot="form">
                <form-item label="">
                    <a-tree-select
                        v-model="form.deptName"
                        allowClear
                        style="width:240px"
                        :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
                        :tree-data="deptList"
                        :replaceFields="{
                            children: 'children',
                            title: 'deptName',
                            key: 'deptId',
                            value: 'deptName'
                        }"
                        placeholder="请选择部门"
                    >
                    </a-tree-select>
                </form-item>
                <form-item label="">
                    <a-month-picker v-model="form.month" placeholder="请选择月份"> </a-month-picker>
                </form-item>
            </template>
            <template slot="control">
                <a-button class="form_btn btn_space" icon="download" @click="download">导出列表</a-button>
            </template>
        </table-search>
        <table-list :loading="loading" :columns="columns" rowKey="id" :pagination="false" :data="list">
            <template slot="status" slot-scope="{ text }">
                <field-tag :value="text" v-if="text" type="value" showType="name" :data="workOrderStatus"></field-tag>
            </template>
        </table-list>
    </a-spin>
</template>
<script>
import table from '@/mixins/table';
import { columns } from './total';
const moment = require('moment');
import { remoteTotalInfo, remoteDownloadWorkorder } from '@/api/operation/workorder';
import { downloadFile } from '@/utils';

export default {
    name: 'TotalInfo',
    mixins: [table],
    props: {
        info: {
            type: Object,
            default: () => {}
        },
        deptList: {
            type: Array,
            default: () => []
        }
    },
    data() {
        let originForm = {
            deptName: undefined,
            month: ''
        };
        return {
            columns,
            originForm,
            spinning: false,
            form: {
                ...originForm
            }
        };
    },
    watch: {
        'form.month': function(e) {
            if (e) {
                let year = moment(e).format('YYYY');
                let month = moment(e).format('MM');
                let fromDate = `${year}/${month}/01`;
                let toDate = moment(fromDate)
                    .endOf('month')
                    .format('YYYY/MM/DD');
                this.form.fromDate = fromDate;
                this.form.toDate = toDate;
            } else {
                delete this.form.fromDate;
                delete this.form.toDate;
            }
        }
    },
    created() {
        this.search();
    },
    methods: {
        async search(page) {
            try {
                this.loading = true;
                if (page) {
                    this.form.page = page;
                }
                let form = { ...this.form };
                delete form.month;
                // if(form.fromDate){
                //     form.fromDate = form.fromDate+' 00:00:00'
                // }
                // if(form.toDate){
                //     form.toDate = form.toDate+' 23:59:59'
                // }
                this.list = [];
                let res = await remoteTotalInfo(form);
                let data = res.data;
                this.list = data;
            } finally {
                this.loading = false;
            }
        },
        //下载统计表
        async download() {
            try {
                this.spinning = true;
                let { deptName, fromDate, toDate } = this.form;
                if (!fromDate) {
                    this.$message.error('请选择导出数据月份');
                    return false;
                }
                let res = await remoteDownloadWorkorder(this.form);
                let text = '月度积分统计表';
                let fileName = deptName
                    ? deptName +
                      moment(fromDate)
                          .endOf('month')
                          .format('YYYY/MM') +
                      text
                    : moment(fromDate)
                          .endOf('month')
                          .format('YYYY/MM') + text;
                downloadFile(res, fileName, 'xlsx');
            } finally {
                this.spinning = false;
            }
        }
    }
};
</script>
